import {Actor, Action} from 'iflux2';
import {fromJS} from 'immutable';
import {GoodsSearchType} from '../constant/action-type';

const defaultParams = {pageNum: 0, pageSize: 12, totalCount: 0};


export default class GoodsSearchActor extends Actor {
    defaultState() {
        return {
            goodsSearch: {
                searchValue: '',
                pagination: defaultParams,
                showType: 'sku'
            }
        }
    }


    @Action(GoodsSearchType.SET_GOODS_SEARCH_TYPE)
    setType(state, type) {
        return state.setIn(['goodsSearch', 'showType'], type);
    }


    @Action(GoodsSearchType.SET_GOODS_SEARCH_VALUE)
    setValue(state, value) {
        return state.setIn(['goodsSearch', 'searchValue'], value);
    }


    @Action(GoodsSearchType.CHANGE_GOODS_SEARCH_PAGINATION)
    changePagination(state, page) {
        return state.updateIn(['goodsSearch', 'pagination'], (pagination)=>pagination.merge(page));
    }


    @Action(GoodsSearchType.RESET_GOODS_SEARCH_PAGINATION)
    resetPagination(state) {
        return state.setIn(['goodsSearch', 'pagination'], fromJS(defaultParams))
    }


    @Action(GoodsSearchType.RESET_GOODS_SEARCH)
    resetGoodsSearch(state) {
        return state.update('goodsSearch', ()=>{
            return fromJS({
                searchValue: '',
                pagination: defaultParams,
                showType: 'sku'
            })
        })
    }
}